import json

import scrapy
from scrapy_redis.spiders import RedisSpider

from DouBan.spiders.comment import CommentSpider


class Comment2Spider(RedisSpider):
    name = "comment_2"
    allowed_domains = ["mtime.com"]
    start_urls = ["http://front-gateway.mtime.com/community/rcmd/ta_shuo.api?returnType=3&pageSize=20&nextStamp=0_1714612791094_36200"]
    redis_key = 'Mtime'

    def __init__(self, *args, **kwargs):
        domain = kwargs.pop('domain', '')
        self.allowed_domains = list(filter(None, domain.split(',')))
        super(Comment2Spider, self).__init__(*args, **kwargs)

    def parse(self, response):
        data = json.loads(response.text)
        data_list = data['data']['rcmdContentList']['items']

        for item_data in data_list:
            item = {}
            item['comment_name'] = item_data['commContent']['title']
            item['comment_content'] = item_data['commContent']['mixWord']

            if 'commContent' in item_data and isinstance(item_data['commContent'], dict):

                if 'fcMovie' in item_data['commContent'] and isinstance(item_data['commContent']['fcMovie'], dict):

                    if 'name' in item_data['commContent']['fcMovie']:

                        item['movie_name'] = item_data['commContent']['fcMovie']['name']
                        item['movie_time'] = item_data['commContent']['fcMovie']['minutes']
                        item['movie_rate'] = item_data['commContent']['fcRating']
                        item['movie_type'] = item_data['commContent']['fcMovie']['genreTypes']
                        item['movie_country'] = item_data['commContent']['fcMovie']['releaseArea']
                        item['kind'] = 'Mtime'

                        yield item
                    else:
                        self.logger.warning("Missing 'name' key in fcMovie for item: %s", item_data)
                else:
                    self.logger.warning("Missing 'fcMovie' key in commContent for item: %s", item_data)
            else:
                self.logger.warning("Missing 'commContent' key in item data: %s", item_data)

        next_num = data['data']['rcmdContentList']['nextStamp']
        yield scrapy.Request(
            url="http://front-gateway.mtime.com/community/rcmd/ta_shuo.api?returnType=3&pageSize=20&nextStamp=" + next_num,
            callback=self.parse
        )